#include "nemu.h"
#include "cpu/exec/helper.h"
#include "cpu/reg.h"
#include "cpu/decode/modrm.h"

make_helper(pop_r_v) {
    uint8_t reg_index = ops_decoded.opcode & 0x7;
    uint32_t value = swaddr_read(cpu.esp, 4);
    cpu.esp += 4;
    reg_l(reg_index) = value;

    print_asm("pop %%%s", regsl[reg_index]);
    return 1;
}

make_helper(pop_rm_v) {
    decode_rm_l(eip + 1);

    uint32_t value = swaddr_read(cpu.esp, 4);
    cpu.esp += 4;

    if (op_dest->type == OP_TYPE_REG) {
        reg_l(op_dest->reg) = value;
    }
    else if (op_dest->type == OP_TYPE_MEM) {
        swaddr_write(op_dest->addr, 4, value);
    }
    else {
        assert(0);
    }

    print_asm("pop %s", op_dest->str);
    return 2;
}